package learnonline.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import learnonline.dao.StudentDao;
import learnonline.dao.UserDao;
import learnonline.dao.impl.UserDaoImpl;
import learnonline.entity.Student;
import learnonline.entity.User;
@WebServlet("/Check-Login")
public class LoginServlet extends HttpServlet{
	UserDao userDao = new UserDaoImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String userName=req.getParameter("userName");
		User user=userDao.checkUser(userName);
		PrintWriter out=resp.getWriter();
		out.print(user!=null);
		out.flush();
		out.close();		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String psw=req.getParameter("passWord");
		String userName=req.getParameter("userName");
		User user=userDao.UserConfirm(userName, psw);
		int authority =userDao.checkUser(userName).getAuthority();
		System.out.println(authority);
		if(user!=null){
			String name=user.getSname();
			req.getSession().setAttribute("LoginUser", user);
			req.getSession().setAttribute("username", name);
			req.getSession().setAttribute("authority", authority);
			resp.sendRedirect("index");
		}else{
			req.getRequestDispatcher("Login.jsp").forward(req, resp);
		}
	}
}
